<!--我的信息填报-->
<template>
  <div id="businessFormValue" class="app-container">

    <!--搜索条件-->
    <div class="filter-container">
      <el-form :model="listQuery" :inline="true">
        <el-form-item>
          <category-dict-select v-model="listQuery.categoryId" type="06" :check-strictly="false" :is-show-all="true" placeholder="请选择分类" :refresh="false" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="listQuery.formName" clearable placeholder="请输入填报表单" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" plain @click="handleFilter">搜索</el-button>
          <el-button icon="el-icon-refresh" plain @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--列表-->
    <div class="table-container flex-main">
      <div class="table-container-header">
        <table-toolbar :create="false" @refresh="handleFilter" @remove="removeRecord" />
      </div>
      <el-table
        ref="multipleTable"
        :data="list"
        header-cell-class-name="table-header"
        fit
        height="100%"
        highlight-current-row
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
        @sort-change="sortChange">
        <el-table-column type="selection" width="60" />
        <el-table-column align="center" prop="id" label="#" width="60">
          <template slot-scope="scope">{{ scope.$index+1 }}</template>
        </el-table-column>
        <el-table-column prop="createdBy" label="提交信息" min-width="230" :show-overflow-tooltip="true">
          <template slot-scope="scope">{{ scope.row.gmtCreated | formatDate }}提交的{{ scope.row._fullCategoryName }}{{ scope.row.formName }}信息</template>
        </el-table-column>
        <el-table-column label="手机号" prop="createdUserMobile" width="100" show-overflow-tooltip />
        <el-table-column label="操作" align="center" fixed="right" width="90">
          <template slot-scope="scope">
            <button-option icon="el-icon-edit" skin="primary" title="编辑" @click="handleUpdate(scope.row)" />
            <button-option icon="el-icon-delete" skin="danger" title="删除" @click="handleRemoveSingle(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination_handler">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
    </div>

    <model-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="800px"
      @close="dialogVisible=!dialogVisible">
      <div style="padding:20px 0">
        <form-create
          ref="formCreater"
          v-model="formCreate"
          :rule="rule"
          :option="option"
          :value.sync="value" />
      </div>
      <template slot="footer">
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </template>
    </model-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
export default {
  components: {},

  data() {
    return {
      nodeSelectedKey: '',
      defaultTreeProps: {
        children: 'children',
        label: 'label',
        isLeaf: 'leaf'
      },
      searchValue: '',
      treeDatas: [],
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        formName: null,
        categoryId: '-',
        sort: this.common.SORT_DEFAULT
      },

      multipleSelectionRecords: [],
      formValueRecord: {},
      tableColumns: [],
      formCreate: {},
      value: {},
      rule: [],
      option: {
        submitBtn: false,
        onSubmit: function(formData) {
        }
      },
      dialogTitle: '',
      dialogVisible: false
    }
  },
  watch: {
    searchValue(val) {
      this.$refs.formTree.filter(val)
    }
  },
  created() {
  },
  methods: {
    // 表格自带方法开始
    /* 全选/反选*/
    handleSelectionChange(val) {
      this.multipleSelectionRecords = val
    },
    /* 表格行数据单击*/
    handleRowClick(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    /* 表格列排序*/
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = this.common.SORT_DEFAULT
      }
      this.handleFilter()
    },
    // 表格自带方法结束

    /* 搜索*/
    handleFilter() {
      this.listQuery = {
        ...this.listQuery,
        page: 1
      }
      this.fetchData()
    },
    /* 重置*/
    handleReset() {
      this.listQuery = {
        ...this.listQuery,
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        formName: null,
        categoryId: '-',
        sort: this.common.SORT_DEFAULT
      }
      this.$refs.multipleTable.clearFilter()
      this.$refs.multipleTable.clearSort()
      this.fetchData()
    },
    async fetchData() {
      const response = await this.$store.dispatch('businessFormValuePageV2', this.listQuery)
      this.list = []
      response.records.forEach(item => {
        var obj = { ...item }
        if (item.formValue) {
          var json = JSON.parse(item.formValue)
          for (var index in json) {
            obj[index] = json[index]
          }
        }
        this.list.push(obj)
      })
      this.total = response.total
    },
    handleUpdate(row) {
      this.formValueRecord = Object.assign({}, row)
      this.$store.dispatch('businessFormValueGetDetailsById', row.id).then((res) => {
        console.log(res)
        this.dialogTitle = `【${res.formName}】信息填报`
        if (res.formOptions) {
          this.option = JSON.parse(res.formOptions)
        } else {
          this.option = {}
        }
        if (res.formContent) {
          this.dialogVisible = true
          res.formContent = res.formContent.replaceAll('${fileServer}', this.$FILE_SERVER + 'file/upload?catalogId=' + this.constants.FILE_CATALOG_FORM_DESIGN)
          res.formContent = res.formContent.replaceAll('${ProjectsId}', this.constants.PROJECTS_ID)
          res.formContent = res.formContent.replaceAll('${Access-Token}', getToken())
          this.rule = JSON.parse(res.formContent)
        } else {
          this.msgWarning('表单缺失')
        }
        if (res.formValue) {
          this.value = JSON.parse(res.formValue)
          console.log(this.value)
        }
      }).catch((_) => {})
    },
    handleSubmit() {
      this.formCreate.validate((validate) => {
        if (validate) {
          this.formCreate.submit((formData) => {
            this.$store.dispatch('businessFormValueUpdate', {
              id: this.formValueRecord.id,
              formId: this.formValueRecord.formId,
              formValue: JSON.stringify(formData)
            }).then(response => {
              this.dialogVisible = false
              this.$modal.msgSuccess('修改成功')
              this.fetchData()
            }).catch(() => {
              this.dialogVisible = true
            })
          })
        }
      })
    },
    /* 删除*/
    handleRemoveSingle(row) {
      this.multipleSelectionRecords = []
      this.multipleSelectionRecords.push(row)
      this.$refs.multipleTable.clearSelection()
      this.$refs.multipleTable.toggleRowSelection(row, true)
      this.removeRecord()
    },
    removeRecord() {
      if (this.multipleSelectionRecords.length > 0) {
        const ids = []
        this.multipleSelectionRecords.forEach(function(v, i) {
          ids.push(v.id)
        })
        this.$confirm('此操作将永久删除记录, 是否继续?', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('businessFormValueRemove', ids).then(response => {
            this.handleReset()
            this.$modal.msgSuccess('删除成功')
          }).catch(() => {})
        }).catch(() => { })
      } else {
        this.$modal.msgWarning('请选择要删除的记录')
      }
    }

  }
}
</script>
<style lang="scss" scoped>
</style>
